package jimo.SSMStudent.controller;

import jimo.SSMStudent.pojo.Users;
import jimo.SSMStudent.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/***
 * 关于用户的控件
 */
@Controller
@RequestMapping("stu")
public class UserController {
@Autowired
private UserServiceImpl userService;
    @RequestMapping(value = "login",method = RequestMethod.GET)
    public ModelAndView login(HttpServletRequest req,Users user) throws IOException {
        //接受接受参数
        ModelAndView mv = new ModelAndView();
        //处理乱码
        System.out.println("获取前端信息：user======"+user);
        //调取service
        Users users = userService.longin(user.getLoginname(),user.getPassword());
        //调取页面
        //PrintWriter writer = resp.getWriter();
        if (users==null){
            //弹窗方式提示用户登录失败
            /*resp.setContentType("text/html;charset=utf-8");*/
           /* writer.println("<script>location.href='/studentIndex/login.jsp';alert('输入有误！！！');</script>");*/
            mv.setViewName("login");
            mv.addObject("zt",false);
            System.out.println("mv------>"+mv.toString());
            return mv;
        }else {
           req.getSession().setAttribute("u1", users);
           /* writer.println("<script>location.href='/studentIndex/index.jsp';alert('登录成功！！！');</script>");*/
            mv.addObject("zt",true);
            mv.setViewName("index");
            System.out.println("mv------>"+mv.toString());
            return mv;
        }
    }
    @RequestMapping(value = "loginout" , method = RequestMethod.GET)
    public void loginOut(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //清除session
        req.getSession().invalidate();
        //重定向页面只能退出一部分框架
        //resp.sendRedirect("/login.jsp");
        /*resp.setContentType("text/html;charset=utf-8");*/
        // window.document.location.href="userlogin.html";
        resp.getWriter().println("<script>alert('退出成功！');top.location.href='/studentIndex/login.jsp';</script>");
    }
}
